﻿using CCF20Model;
using WiseLink.RDS.Resource;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace CCF20Module.DeviceState
{
    public interface IDeviceState
    {
        string runInStock();
        string runSellToCustomer(int customerId);
        string runRegisterIDC();
        string runRegisterIDC(string SN, string SNKey, string IDSName, string IDSID);
        string runUnRegisterIDC();
        string runRegisterSpeicalIDC();
        string runUnRegisterSpeicalIDC();
        string runExchangeDevice(string idc,string deviceKey);
        string runReturnDevice();
        string runRepairStart();
        string runRepairEnd();
        string runDeleteDevice();
        string StateName { get; set; }
        void setState(IDeviceState deviceState);
    }

    public class DeviceStateBase : IDeviceState
    {
        protected IDeviceHandler deviceHandler;
        protected DeviceModel deviceModel;
        protected VehicleModel vehicleModel;
        protected IList deviceList;
        public string StateName { get; set; }
        protected eRDSZIC ccfZic;
        protected string failReason;
        protected Device currentDevice;

        public DeviceStateBase(IDeviceHandler handler, DeviceModel model, VehicleModel vModel,Device device, IList list)
        {
            deviceHandler = handler;
            vehicleModel = vModel;
            deviceModel = model;
            currentDevice = device;
            deviceList = list;
        }

        public virtual string runInStock()
        {
            return string.Format(Message.Device_CantInStock, deviceHandler.CurrentDevice.IDCSimNumber,failReason);
        }

        public virtual string runSellToCustomer(int customerId)
        {
            return string.Format(Message.Device_CantSell, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }
        public virtual string runRegisterIDC()
        {
            return string.Format(Message.Device_CantRegisterIDC, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }
        public virtual string runRegisterIDC(string SN, string SNKey, string IDSName, string IDSID)
        {
            return string.Format(Message.Device_CantRegisterIDC, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }
        public virtual string runUnRegisterIDC()
        {
            return string.Format(Message.Device_CantUnRegisterIDC, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }

        public virtual string runRegisterSpeicalIDC()
        {
            return string.Format(Message.Device_CantSpecialIDC, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }
        public virtual string runUnRegisterSpeicalIDC()
        {
            return string.Format(Message.Device_CantUnSpecialIDC, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }
        public virtual string runExchangeDevice(string idc, string deviceKey)
        {
            if (!IsSavedDevice(out failReason))
            {
                return failReason;
            }

            string oldIdc = deviceHandler.CurrentDevice.IDCSimNumber;
            string oldKey = deviceHandler.CurrentDevice.DeviceKey;
            if (oldIdc == idc)
            {
                return Message.Device_CantExchange_SameIDC;
            }

            if (Utility.ShowConfirmation(string.Format(Message.Device_ConfirmExchange, oldIdc)))
            {

                Device itemToSelect = ((IList<Device>)deviceList).AsQueryable().Where(d => d.IDCSimNumber == idc && d.DeviceKey == deviceKey).FirstOrDefault();
                if (itemToSelect == null)
                {
                   return (string.Format(Message.Device_CantExchagne_NotInSalesList, idc));

                }else if (itemToSelect.DeviceStatus ==  DeviceStatus.Sold)
                {
                    return (string.Format(Message.Device_CantExchagne_Sold, itemToSelect.IDCSimNumber));
                }else if (itemToSelect.DeviceStatus == DeviceStatus.Repair)
                {
                    return (string.Format(Message.Device_CantExchagne_Repair, itemToSelect.IDCSimNumber));
                }

                //if (ccfZic.ReturnProdcut(oldIdc, oldKey, idc, deviceKey))
                //{
                //    itemToSelect.CustomerID = deviceHandler.CurrentDevice.CustomerID;
                //    itemToSelect.VehicleID = deviceHandler.CurrentDevice.VehicleID;
                //    itemToSelect.DeviceStatus = deviceHandler.CurrentDevice.DeviceStatus;
                //    itemToSelect.IsRegistered = deviceHandler.CurrentDevice.IsRegistered;
                    
                //    itemToSelect.IsSpecialIDC = deviceHandler.CurrentDevice.IsSpecialIDC;
                //    itemToSelect.IsManual = deviceHandler.CurrentDevice.IsManual;
                //    //itemToSelect.IsDefected = deviceHandler.CurrentDevice.IsDefected;
                //    itemToSelect.IsTransfer = deviceHandler.CurrentDevice.IsTransfer;
                //    itemToSelect.RegisterDate = deviceHandler.CurrentDevice.RegisterDate;
                //    itemToSelect.SaleDate = deviceHandler.CurrentDevice.SaleDate;
                //    itemToSelect.Sales = deviceHandler.CurrentDevice.Sales;
                //    itemToSelect.UsageStatus = deviceHandler.CurrentDevice.UsageStatus;
                //    itemToSelect.SimCard = deviceHandler.CurrentDevice.SimCard;
                //    itemToSelect.IDCSimNumber = deviceHandler.CurrentDevice.IDCSimNumber;
                //    //itemToSelect.DeviceKey = deviceHandler.CurrentDevice.DeviceKey;
                //    //itemToSelect.DeviceType = deviceHandler.CurrentDevice.DeviceType;
                    
                //    deviceHandler.CurrentDevice.CustomerID = null;
                //    deviceHandler.CurrentDevice.VehicleID = null;
                //    deviceList.Remove(deviceHandler.CurrentDevice);
                //    deviceModel.Update();
                //    return string.Format(Message.Device_Exchagne_Succeed, oldIdc, idc);
                //}
                //else
                //{
                //    return string.Format(Message.Device_CantExchagne_ZICError, oldIdc, idc);
                //}
                return null;

            }
            else
            {
                return string.Empty;
            }
        }

        public virtual string runReturnDevice()
        {
            if (!IsSavedDevice(out failReason))
            {
                return failReason;
            }

            
            string idc = deviceHandler.CurrentDevice.IDCSimNumber;

            
            if (Utility.ShowConfirmation(string.Format(Message.Device_Refund_Confirm, idc)))
            {

                //if (ccfZic.RemoveSaleList(deviceHandler.CurrentDevice.DeviceKey, idc))
                //{
                //    deviceList.Remove(deviceHandler.CurrentDevice);
                //    deviceModel.Update();
                //    return string.Format(Message.Device_Refund_Succeed, idc);
                //}
                //else
                //{
                //    return string.Format(Message.Device_Refund_ZICError, idc);
                //}
                return null;
            }
            else
            {
                return string.Empty;
            }
            //return string.Format("设备:{0}无法退货.{1}", deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }
        public virtual string runRepairStart()
        {
            if (!IsSavedDevice(out failReason))
            {
                return failReason;
            }
            if (Utility.ShowConfirmation(string.Format(Message.Device_Repair_Confirm, currentDevice.IDCSimNumber)))
            {
                currentDevice.PreviousDeviceStatus = currentDevice.DeviceStatus;
                currentDevice.DeviceStatus = DeviceStatus.Repair;
                currentDevice.IsDefected = true;
                deviceModel.Update();
                deviceHandler.setState(deviceHandler.getDefectedState());
                return string.Format(Message.Device_Repair_Succeed,currentDevice.IDCSimNumber);
            }
            else
            {
                return string.Empty;
            }
        }

        public virtual string runRepairEnd()
        {
            return string.Format(Message.Device_Repair_CantFinish, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }

        public virtual string runDeleteDevice()
        {
            return string.Format(Message.Device_Cant_Delete, deviceHandler.CurrentDevice.IDCSimNumber, failReason);
        }

        public void setState(IDeviceState deviceState)
        {
            deviceHandler.CurrentState = deviceState;
            LoggerHelper.LogCCF20(string.Format("进入{0} IDC:{1}",deviceState.StateName,currentDevice.IDCSimNumber));
        }
        protected string DeleteDevice()
        {
            string idc = currentDevice.IDCSimNumber;
            

            if (Utility.ShowConfirmation(string.Format(Message.Device_Delete_Confirm, idc)))
            {
                deviceList.Remove(currentDevice);
                deviceModel.Update();
                return string.Format(Message.Device_Delete_Succeed, idc);
            }
            else
            {
                return string.Empty;
            }
            
            
        }

        protected bool ShowConfirmationMessage(string msg)
        {
             

             if (Utility.ShowConfirmation(msg))
             {
                 return true;
             }
             else
             {
                 return false;
             }
        }
        protected bool IsSavedDevice(out string result)
        {
            result = null;
            if (currentDevice.DeviceID == 0)
            {
                result = string.Format(Message.Device_Save_First, currentDevice.IDCSimNumber); ;
                return false;
            }
            else
            {
                return true;
            }
        }
        protected void InitializeVehicleState()
        {
            currentDevice.Vehicle.PreviousContactResult = string.Empty;
            currentDevice.Vehicle.LastContactResult = WiseLink.RDS.Resource.WiseLinkRDSStatus.Contact_NotDecidedYet;
            currentDevice.Vehicle.LastContactTime = null;
            currentDevice.Vehicle.LastServcieDate = null;
            currentDevice.Vehicle.LatestActiveTime = null;
            currentDevice.Vehicle.IsFault = false;
            currentDevice.Vehicle.IsMaintenance = false;
            currentDevice.Vehicle.IsUrgent = false;
                
        }
    }

   
}
